<?php

namespace app\api\controller;
use think\Lang;
/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 退货控制器
 */
class Memberreturn extends MobileMember {

    public function _initialize() {
        parent::_initialize(); // TODO: Change the autogenerated stub
        Lang::load(APP_PATH . 'home/lang/'.config('default_lang').'/memberreturn.lang.php');
    }

    /**
     * 发货
     *
     */
    public function ship_form() {
        $express_list = array();
        $list = rkcache('express', true);
        foreach ($list as $k => $v) {
            $val = array();
            $val['express_id'] = $v['express_id'];
            $val['express_name'] = $v['express_name'];
            $express_list[] = $val;
        }
        $trade_model = model('trade');
        $return_delay = $trade_model->getMaxDay('return_delay'); //发货默认5天后才能选择没收到
        $return_confirm = $trade_model->getMaxDay('return_confirm'); //商家不处理收货时按同意并弃货处理

        $return_id = intval(input('param.return_id'));
        $result = array(
            'return_id' => $return_id, 'return_delay' => $return_delay, 'return_confirm' => $return_confirm,
            'express_list' => $express_list
        );
        ds_json_encode(10000, '', $result);
    }


    /**
     * @api {POST} api/Memberreturn/ship_post 发货保存
     * @apiVersion 1.0.0
     * @apiGroup Memberreturn
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} return_id 退款ID
     * @apiParam {Int} express_id 物流公司ID
     * @apiParam {String} invoice_no 物流单号
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     */ 
    public function ship_post() {
        $refundreturn_model = model('refundreturn');
        $member_id = $this->member_info['member_id'];
        $condition = array();
        $condition['buyer_id'] = $member_id;
        $condition['seller_state'] = 2;
        $condition['goods_state'] = 1;
        $condition['refund_id'] = intval(input('post.return_id'));
        $return = $refundreturn_model->getRefundreturnInfo($condition);
        if (!empty($return)) {
            $refund_array = array();
            $refund_array['ship_time'] = time();
            $refund_array['delay_time'] = time();
            $refund_array['express_id'] = input('post.express_id');
            $refund_array['invoice_no'] = input('post.invoice_no');
            $refund_array['goods_state'] = '2';
            $state = $refundreturn_model->editRefundreturn($condition, $refund_array);
            if ($state) {
                ds_json_encode(10000, '', 1);
            } else {
                ds_json_encode(10001,lang('ds_common_op_fail'));
            }
        } else {
            ds_json_encode(10001,lang('param_error'));
        }
    }

    /**
     * 延迟收货时间
     *
     */
    public function delay_form() {
        $trade_model = model('trade');
        $return_delay = $trade_model->getMaxDay('return_delay'); //发货默认5天后才能选择没收到
        $return_confirm = $trade_model->getMaxDay('return_confirm'); //商家不处理收货时按同意并弃货处理

        $return_id = intval(input('param.return_id'));
        $result = array(
            'return_id' => $return_id, 'return_delay' => $return_delay, 'return_confirm' => $return_confirm
        );
        ds_json_encode(10000, '', $result);
    }


    /**
     * @api {POST} api/Memberreturn/delay_post 延迟收货时间保存
     * @apiVersion 1.0.0
     * @apiGroup Memberreturn
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} return_id 退款ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     */ 
    public function delay_post() {
        $refundreturn_model = model('refundreturn');
        $member_id = $this->member_info['member_id'];
        $condition = array();
        $condition['buyer_id'] = $member_id;
        $condition['seller_state'] = 2;
        $condition['goods_state'] = 3;
        $condition['refund_id'] = intval(input('post.return_id'));
        $return = $refundreturn_model->getRefundreturnInfo($condition);
        if (!empty($return)) {
            $refund_array = array();
            $refund_array['delay_time'] = time();
            $refund_array['goods_state'] = '2';
            $state = $refundreturn_model->editRefundreturn($condition, $refund_array);
            if ($state) {
                ds_json_encode(10000, '', 1);
            } else {
                ds_json_encode(10001,lang('ds_common_op_fail'));
            }
        } else {
            ds_json_encode(10001,lang('param_error'));
        }
    }


    /**
     * @api {POST} api/Memberreturn/get_return_list 退货记录列表
     * @apiVersion 1.0.0
     * @apiGroup Memberreturn
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} page 页码
     * @apiParam {Int} per_page 每页数量
     * @apiParam {String} type 查询字段 order_sn订单号 refund_sn退款单号 goods_name商品名
     * @apiParam {String} k 查询关键词
     * @apiParam {String} add_time_from 时间从 YYYY-MM-DD
     * @apiParam {String} add_time_to 时间到 YYYY-MM-DD
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object[]} result.return_list  退货列表
     * @apiSuccess {Int} result.return_list.add_time  添加时间
     * @apiSuccess {String} result.return_list.admin_state  管理员状态描述
     * @apiSuccess {Int} result.return_list.admin_state_v  管理员处理状态 1:待审核 2:同意 3:不同意
     * @apiSuccess {Int} result.return_list.delay_state  延期状态
     * @apiSuccess {Int} result.return_list.goods_id  商品ID
     * @apiSuccess {String} result.return_list.goods_img_480  商品缩略图完整路径
     * @apiSuccess {String} result.return_list.goods_name  商品名称
     * @apiSuccess {String} result.return_list.goods_num  购买数量
     * @apiSuccess {String} result.return_list.goods_state_v  物流状态 1:待发货,2:待收货,3:未收到,4:已收货
     * @apiSuccess {Int} result.return_list.order_id  订单ID
     * @apiSuccess {String} result.return_list.order_sn  订单编号
     * @apiSuccess {Float} result.return_list.refund_amount  退款金额
     * @apiSuccess {Int} result.return_list.refund_id  退款ID
     * @apiSuccess {String} result.return_list.refund_sn  退款编号
     * @apiSuccess {Int} result.return_list.return_type  退货类型 1:不用退货,2:需要退货
     * @apiSuccess {String} result.return_list.seller_state  卖家处理状态描述 1:待审核,2:同意,3:不同意
     * @apiSuccess {Int} result.return_list.ship_state  发货状态
     * @apiSuccess {Int} result.return_list.seller_state_v  卖家状态
     * @apiSuccess {Int} result.return_list.store_id  店铺ID
     * @apiSuccess {String} result.return_list.store_name  店铺描述
     * @apiSuccess {Int} result.page_total  总页数
     * @apiSuccess {Boolean} result.hasmore  是否有更多 true是false否
     */ 
    public function get_return_list() {
        $order_model = model('order');
        $refundreturn_model = model('refundreturn');
        $member_id = $this->member_info['member_id'];
        $refund_list = array();
        $condition = array();
        $condition['buyer_id'] = $member_id;
        $keyword_type = array('order_sn', 'refund_sn', 'goods_name');
        if (trim(input('param.k')) != '' && in_array(input('param.type'), $keyword_type)) {
            $type = input('param.type');
            $condition[$type] = array('like', '%' . input('param.k') . '%');
        }
        if (trim(input('param.add_time_from')) != '' || trim(input('param.add_time_to')) != '') {
            $add_time_from = strtotime(trim(input('param.add_time_from')));
            $add_time_to = strtotime(trim(input('param.add_time_to')));
            if ($add_time_from !== false || $add_time_to !== false) {
                $condition['add_time'] = array('time', array($add_time_from, $add_time_to));
            }
        }
        $list = $refundreturn_model->getReturnList($condition, $this->pagesize);
        if (!empty($list) && is_array($list)) {
            $seller_state = $this->getRefundStateArray('seller');
            $admin_state = $this->getRefundStateArray('admin');
            $rec_id_list = array();
            foreach ($list as $k => $v) {
                $rec_id_list[] = $v['order_goods_id'];
            }
            $spec_list = array();
            $goods_list = array();
            $goods_list = $order_model->getOrdergoodsList(array('rec_id' => array('in', $rec_id_list)));

            /* foreach ($goods_list as $k => $v) {
              $order_goods_id = $v['goods_id'];
              $spec_list[$order_goods_id] = $v['goods_spec'];
              } */
            foreach ($list as $k => $v) {
                $val = array();
                $val['refund_id'] = $v['refund_id'];
                $val['goods_id'] = $v['goods_id'];
                $val['goods_name'] = $v['goods_name'];
                //$val['goods_spec'] = $spec_list[$v['order_goods_id']]['goods_spec'];
                $val['goods_num'] = $v['goods_num'];
                $val['goods_state_v'] = $v['goods_state']; //物流状态:1为待发货,2为待收货,3为未收到,4为已收货
                $val['ship_state'] = '0';
                $val['delay_state'] = '0';
                if ($v['seller_state'] == 2 && $v['return_type'] == 2 && $v['goods_state'] == 1) {
                    $val['ship_state'] = '1';
                }
                if ($v['seller_state'] == 2 && $v['return_type'] == 2 && $v['goods_state'] == 3) {
                    $val['delay_state'] = '1';
                }
                $val['order_id'] = $v['order_id'];
                $val['refund_amount'] = ds_price_format($v['refund_amount']);
                $val['refund_sn'] = $v['refund_sn'];
                $val['return_type'] = $v['return_type'];
                $val['order_sn'] = $v['order_sn'];
                $val['add_time'] = date("Y-m-d H:i:s", $v['add_time']);
                $val['goods_img_480'] = goods_thumb($v, 480);
                $val['seller_state_v'] = $v['seller_state'];
                $val['seller_state'] = $seller_state[$v['seller_state']];
                $val['admin_state_v'] = $v['refund_state'];
                $val['admin_state'] = $v['seller_state'] == 2 ? $admin_state[$v['refund_state']] : lang('none');
                $val['store_id'] = $v['store_id'];
                $val['store_name'] = $v['store_name'];
                $refund_list[] = $val;
            }
        }

        $result = array_merge(array('return_list' => $refund_list), mobile_page($refundreturn_model->page_info));
        ds_json_encode(10000, '', $result);
    }
    
    /**
     * @api {POST} api/Memberreturn/get_express 获取物流公司
     * @apiVersion 1.0.0
     * @apiGroup Memberreturn
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object[]} result.express_list  物流公司列表
     * @apiSuccess {String} result.express_list.express_code  物流公司代码
     * @apiSuccess {Int} result.express_list.express_id  物流公司ID
     * @apiSuccess {String} result.express_list.express_letter  物流公司首字母
     * @apiSuccess {String} result.express_list.express_name  物流公司名称
     * @apiSuccess {String} result.express_list.express_order  排序 1:常用2:不常用
     * @apiSuccess {String} result.express_list.express_state  状态 0:不可用1:可用
     * @apiSuccess {String} result.express_list.express_url  官网
     * @apiSuccess {Int} result.express_list.express_zt_state  是否支持服务站配送 0否1是
     */ 
    public function get_express(){
        $express_list = rkcache('express', true);
        ds_json_encode(10000, '', array('express_list'=>$express_list,'express_list_app'=>array_values($express_list)));
    }


    /**
     * @api {POST} api/Memberreturn/get_return_info 查看退货信息
     * @apiVersion 1.0.0
     * @apiGroup Memberreturn
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} return_id 退款ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {String[]} result.pic_list  退货凭证列表
     * @apiSuccess {Object} result.return_info  退货信息
     * @apiSuccess {Int} result.return_info.add_time  添加时间
     * @apiSuccess {String} result.return_info.admin_state  管理员状态描述
     * @apiSuccess {Int} result.return_info.admin_state_v  管理员处理状态 1:待审核 2:同意 3:不同意
     * @apiSuccess {Int} result.return_info.delay_state  延期状态
     * @apiSuccess {Int} result.return_info.goods_id  商品ID
     * @apiSuccess {String} result.return_info.goods_img_480  商品缩略图完整路径
     * @apiSuccess {String} result.return_info.goods_name  商品名称
     * @apiSuccess {String} result.return_info.goods_num  购买数量
     * @apiSuccess {String} result.return_info.goods_state_v  物流状态 1:待发货,2:待收货,3:未收到,4:已收货
     * @apiSuccess {Int} result.return_info.order_id  订单ID
     * @apiSuccess {String} result.return_info.order_sn  订单编号
     * @apiSuccess {Float} result.return_info.refund_amount  退款金额
     * @apiSuccess {Int} result.return_info.refund_id  退款ID
     * @apiSuccess {String} result.return_info.refund_sn  退款编号
     * @apiSuccess {Int} result.return_info.return_type  退货类型 1:不用退货,2:需要退货
     * @apiSuccess {String} result.return_info.seller_state  卖家处理状态描述 1:待审核,2:同意,3:不同意
     * @apiSuccess {Int} result.return_info.ship_state  发货状态
     * @apiSuccess {Int} result.return_info.seller_state_v  卖家状态
     * @apiSuccess {Int} result.return_info.store_id  店铺ID
     * @apiSuccess {String} result.return_info.store_name  店铺描述
     */ 
    public function get_return_info() {
        $refundreturn_model = model('refundreturn');
        $member_id = $this->member_info['member_id'];
        $condition = array();
        $condition['buyer_id'] = $member_id;
        $condition['refund_id'] = intval(input('param.return_id'));
        $refund_info = $refundreturn_model->getRefundreturnInfo($condition);
        if (!empty($refund_info) && is_array($refund_info)) {
            $seller_state = $this->getRefundStateArray('seller');
            $admin_state = $this->getRefundStateArray('admin');
            $refund = array();
            $refund['refund_id'] = $refund_info['refund_id'];
            $refund['goods_id'] = $refund_info['goods_id'];
            $refund['goods_name'] = $refund_info['goods_name'];
            $refund['goods_num'] = $refund_info['goods_num'];
            $refund['goods_state_v'] = $refund_info['goods_state'];
            $refund['ship_state'] = '0';
            $refund['delay_state'] = '0';
            if ($refund_info['seller_state'] == 2 && $refund_info['return_type'] == 2 && $refund_info['goods_state'] == 1) {
                $refund['ship_state'] = '1';
            }
            if ($refund_info['seller_state'] == 2 && $refund_info['return_type'] == 2 && $refund_info['goods_state'] == 3) {
                $refund['delay_state'] = '1';
            }
            $express_list = rkcache('express', true);
            if ($refund_info['express_id'] > 0 && !empty($refund_info['invoice_no'])) {
                $refund['express_name'] = $express_list[$refund_info['express_id']]['express_name'];
                $refund['invoice_no'] = $refund_info['invoice_no'];
            }
            $refund['order_id'] = $refund_info['order_id'];
            $refund['refund_amount'] = ds_price_format($refund_info['refund_amount']);
            $refund['refund_sn'] = $refund_info['refund_sn'];
            $refund['return_type'] = $refund_info['return_type'];
            $refund['order_sn'] = $refund_info['order_sn'];
            $refund['add_time'] = date("Y-m-d H:i:s", $refund_info['add_time']);
            $refund['goods_img_480'] = goods_thumb($refund_info, 480);
            $refund['seller_state'] = $seller_state[$refund_info['seller_state']];
            $refund['admin_state'] = $refund_info['seller_state'] == 2 ? $admin_state[$refund_info['refund_state']] : lang('none');
            $refund['store_id'] = $refund_info['store_id'];
            $refund['store_name'] = $refund_info['store_name'];
            $refund['reason_info'] = $refund_info['reason_info'];
            $refund['buyer_message'] = $refund_info['buyer_message'];
            $refund['seller_message'] = $refund_info['seller_message'];
            $refund['admin_message'] = $refund_info['admin_message'];

            $info['buyer'] = array();
            if (!empty($refund_info['pic_info'])) {
                $info = unserialize($refund_info['pic_info']);
            }
            $pic_list = array();
            if (is_array($info['buyer']) && !empty($info['buyer'])) {
                foreach ($info['buyer'] as $k => $v) {
                    if (!empty($v)) {
                        $pic_list[] = UPLOAD_SITE_URL . '/' . ATTACH_PATH . '/refund/' . $v;
                    }
                }
            }
            ds_json_encode(10000, '', array('return_info' => $refund, 'pic_list' => $pic_list));
        } else {
            ds_json_encode(10001,lang('param_error'));
        }
    }

    /* 退款审核状态 */

    function getRefundStateArray($type = '') {

        if ($type == 'seller') {
            $state_array = array(
                '1' => lang('refund_state_confirm'), '2' => lang('refund_state_yes'), '3' => lang('refund_state_no')
            ); //卖家处理状态:1为待审核,2为同意,3为不同意
            return $state_array;
        };
        if ($type == 'admin') {
            $admin_array = array(
                '1' => lang('in_processing'), '2' => lang('to_be_processed'), '3' => lang('has_been_completed')
            ); //确认状态:1为买家或卖家处理中,2为待平台管理员处理,3为退款退货已完成
            return $admin_array;
        }
    }

}
